﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;
using System.ComponentModel;
using System.Collections;
using System.Drawing.Design;
using System.Data;
using Water.Util;

namespace Water.Web.Controls.Office.Excel
{
    [ParseChildren(true, "Sheets")]
    [ToolboxData("<{0}:Report runat=server></{0}:Report>")]
    public partial class Report :WebControl,INamingContainer     
    {
        private SheetItemCollection sheets;
        private string template = "default.xlt";

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Script;
            }
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            writer.AddAttribute("language", "VBScript");
        }

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
                base.RenderBeginTag(writer);
                writer.Write(Water.Web.Controls.Office.Excel.Util.Report.CreateExcelObject(this.TemplateUrl));
        }

        public override void RenderEndTag(HtmlTextWriter writer)
        {
                writer.Write(Water.Web.Controls.Office.Excel.Util.Report.WindowColse());
                base.RenderEndTag(writer);
        }

        [Browsable(true),Category("Data"), GlobalizeDescription("Items"), NotifyParentProperty(true), TypeConverter(typeof(CollectionConverter))]
        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public SheetItemCollection Sheets
        {
            get 
            {
                if (sheets == null)
                    sheets = new SheetItemCollection();
                return sheets; 
            }
        }

        [Browsable(true), Category("Behavior"), GlobalizeDescription("Template"), DefaultValue("default.xlt")]
        public string Template
        {
            get { return template; }
            set { template = value; }
        }

        [Browsable(false)]
        private string TemplateUrl
        {
            get
            {
                string _appPath = this.Page.Request.ApplicationPath;
                string _host = this.Page.Request.Url.Host;
                string _port=this.Page.Request.Url.Port.ToString();
                _port = ":" + _port;
                if (_appPath.EndsWith("/"))
                {
                    _appPath = _appPath.Substring(0, _appPath.Length - 1);
                }
                string _url = "http://"
                    + _host
                    + _port
                    + _appPath
                    + StringUtil.TEMPLATEPATH
                    + Template;
                return _url;
            }
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            HttpResponse response = this.Page.Response;
            HttpRequest request = this.Page.Request;

            string _dataSessionId = request.QueryString[StringUtil.DATASESSIONID];

            if (PageState.State == StringUtil.GETTEXTDATA && !string.IsNullOrEmpty(_dataSessionId))
            {
                object _text = this.Page.Application[_dataSessionId];
                response.ContentType = "text/plain";
                response.Clear();
                response.Charset = "gb2312";
                response.ContentEncoding = Encoding.GetEncoding("gb2312");

                if (null != _text)
                    response.Write(_text.ToString());

                this.Page.Application.Remove(_dataSessionId);//remove

                response.End();
            }
 
            if (PageState.State == StringUtil.EXCELREPORT)
            {
                base.RenderContents(writer);
            }
        }

        protected override void CreateChildControls()
        {
            if (!DesignMode)
            {
                if (PageState.State == StringUtil.EXCELREPORT)
                {
                    if (this.Page.Request.QueryString[StringUtil.REPORTID] == this.ID)
                    {
                        if (Sheets != null && Sheets.Count > 0)
                        {
                            int i = 1;
                            foreach (Sheet _sheet in Sheets)
                            {
                                _sheet.SheetNum = i;
                                this.Controls.Add(_sheet);
                                i++;
                            }
                        }
                    }
                }
            }
        }
        
        public override void RenderControl(HtmlTextWriter writer)
        {
            if ((PageState.State == StringUtil.EXCELREPORT && this.Page.Request.QueryString[StringUtil.REPORTID] == this.ID) || PageState.State == StringUtil.GETTEXTDATA)
            {
                this.Page.Response.Clear();
                base.RenderControl(writer);
                this.Page.Response.End();
            }
        }

    }
}
